package com.njbw.user.enums;

import lombok.Getter;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 渠道枚举
 */
@Getter
public enum ChannelEnum {

    DOU_YIN(0, "抖音渠道"),
    BILIBILI(1, "B站渠道"),
    ;

    private final int code;

    private final String desc;

    ChannelEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static final Map<Integer, ChannelEnum> clientChannelMap = Stream.of(ChannelEnum.values())
            .collect(Collectors.toMap(ChannelEnum::getCode, e -> e));

    public static ChannelEnum getChannel(Integer code) {
        // 适用于枚举里的东西非常多
        return clientChannelMap.get(code);
    }

    /**
     * 根据 code 值获取渠道枚举
     */
    public static ChannelEnum getByCode(int codeVal) {
        for (ChannelEnum channelEnum : ChannelEnum.values()) {
            if (channelEnum.code == codeVal) {
                return channelEnum;
            }
        }
        return null;
    }

    /**
     * 根据 code 值获取 desc
     */
    public static String getDescByCode(int codeVal) {
        ChannelEnum[] channelEnums = ChannelEnum.values();
        for (ChannelEnum channelEnum : channelEnums) {
            if (channelEnum.code == codeVal) {
                return channelEnum.desc;
            }
        }
        return null;
    }
}
